﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLTV_V2.DataAccess;
using QLTV_V2.BussinessLogic;
using QLTV_V2.BussinessObject;

namespace QLTV_V2
{
    public partial class FrmTraSach : DevComponents.DotNetBar.Office2007RibbonForm
    {
        public FrmTraSach()
        {
            InitializeComponent();
        }
        Utilities uti = new Utilities();
        MuonTraBUS mtBUS = new MuonTraBUS();
        PhieuMuonTra phieuMT = new PhieuMuonTra();
        CTMuonTra ctMT = new CTMuonTra();
        private void FrmTraSach_Load(object sender, EventArgs e)
        {
            //Load data column dgv
            TinhTrang.Items.Add("Tốt");
            TinhTrang.Items.Add("Hỏng");
        }

        private void btn_Giahan_Click(object sender, EventArgs e)
        {
            FrmGiahanMT frm = new FrmGiahanMT();
            frm.maMT = txt_MaMT.Text;
            frm.dk = 1;
            frm.ShowDialog();
        }

        private void btn_TraSach_Click(object sender, EventArgs e)
        {
            int count1=dgv_TraSach.Rows.Count-1;
            dgv_TraSach.CurrentCell = dgv_TraSach.Rows[count1].Cells[0];
            dgv_TraSach.CurrentCell = dgv_TraSach.Rows[0].Cells[0];

            int dem = 0;
            int check = 0;
            DateTime ngayTra=System.DateTime.Now;
            int count = dgv_TraSach.Rows.Count - 1;
            for (int i = 0; i < count; i++)
            {
                if (Convert.ToBoolean(dgv_TraSach.Rows[i].Cells["Chon"].Value) == true)
                {
                    check++;
                    string mactMT = dgv_TraSach.Rows[i].Cells["MaCTMuonTra"].Value.ToString();
                    if (mtBUS.update_TraSach(ngayTra, mactMT) == true)
                    {
                        string masach=dgv_TraSach.Rows[i].Cells["MaBanSach"].Value.ToString();
                        string tinhtrang = dgv_TraSach.Rows[i].Cells["TinhTrang"].Value.ToString();
                        if (mtBUS.update_BanSach_MT(masach, tinhtrang, 2) == true)
                            dem++;
                    }
                }
            }
            if (check == 0)
                MessageBox.Show("Chưa chọn Sách để trả");
            else
            {
                if (dem == check)
                {
                    MessageBox.Show("Lưu thông tin thành công!", "Thông báo");
                    dgv_TraSach.DataSource = mtBUS.getCTMuonTra(txt_MaMT.Text);
                }
                else
                    MessageBox.Show("Không thể lưu, kiểm tra lại thông tin","Thông báo");
            }
        }

        private void btn_KiemTra_Click(object sender, EventArgs e)
        {
            string maBanDoc = txt_MaBandoc.Text;           
            if (uti.CheckExitsValue("PhieuMuonTra", "MaBanDoc", maBanDoc) == false)
            {
                if (mtBUS.checkExistMT(maBanDoc) == true)
                {
                    phieuMT = mtBUS.getPhieuMT_Ob_TraSach(maBanDoc);
                    date_Ngaymuon.Value = phieuMT.NgayMuon;
                    date_Ngayphaitra.Value = phieuMT.NgayPhaiTra;
                    txt_MaMT.Text = phieuMT.MaMuonTra;
                    dgv_TraSach.DataSource = mtBUS.getCTMuonTra(phieuMT.MaMuonTra);
                    ////
                    btn_TraSach.Enabled = true;
                    btn_Giahan.Enabled = true;
                    btn_XulyVP.Enabled = true;
                }
                else
                    MessageBox.Show("Sách đã được trả. Kiểm tra lại thông tin", "Thông báo");
            }
            else
                MessageBox.Show("Mã thẻ không đúng");
        }

        private void btn_ChonTatCa_Click(object sender, EventArgs e)
        {
            int count = dgv_TraSach.Rows.Count - 1;
            for (int i = 0; i < count; i++)
                dgv_TraSach.Rows[i].Cells["Chon"].Value = true;
        }

        private void buttonItem2_Click(object sender, EventArgs e)
        {
            int count = dgv_TraSach.Rows.Count - 1;
            for (int i = 0; i < count; i++)
                dgv_TraSach.Rows[i].Cells["Chon"].Value = false;
        }
        private void btn_XulyVP_Click(object sender, EventArgs e)
        {
            FrmXulyVP frm = new FrmXulyVP();
            frm.mabandoc = txt_MaBandoc.Text;
            frm.dk = 1;
            frm.ShowDialog();
        }

        private void btn_Thoat_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn thoát không?", "Hỏi",
         MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                this.Close();
            }
        }

        private void dgv_TraSach_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            uti.dgv_GetSTT_RowHeader(sender, e, dgv_TraSach);
        }
    }
}
